package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.PageDTO;
import com.sky.entity.Category;
import com.sky.query.CategoryQuery;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    @ApiOperation("新增分类接口")
    public Result save(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类：{}",categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分类分页查询接口")
    public Result<PageDTO<Category>> queryPage(CategoryQuery categoryQuery){
        log.info("分类查询条件：{}",categoryQuery);
        PageDTO<Category> pageDTO = categoryService.queryPage(categoryQuery);
        return Result.success(pageDTO);
    }

    @PostMapping("status/{status}")
    @ApiOperation("启用、禁用分类接口")
    public Result startOrStop(@PathVariable Integer status,@RequestParam("id") Long id){
        log.info("根据ID更改分类状态：{}",id);
        categoryService.updateStatus(status,id);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改分类接口")
    public Result update(@RequestBody CategoryDTO categoryDTO){
        log.info("修改分类信息：{}",categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("根据id删除分类")
    public Result delete(@RequestParam("id") Long id){
        categoryService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> queryType(Integer type){
        log.info("根据类型查找分类：{}",type);
        List<Category> categories = categoryService.queryType(type);

        return Result.success(categories);
    }


}
